﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;

namespace Taxes.Users
{
    public partial class UserList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [DirectMethod]
        public string DeleteUser(int id)
        {
            var bll = new BLL.ysys_user();
            if (bll.Delete(id))
                return "1";
            return "0";
        }


        protected void btnAdd_Click(object sender, DirectEventArgs e)
        {
            if (tfLoginName.Text.Trim() == string.Empty)
            {
                X.MessageBox.Configure(new MessageBoxConfig()
                {
                    Icon = MessageBox.Icon.INFO,
                    Title = "提示",
                    Message = "登录名不能为空"
                }).Show();
                return;
            }
            if (tfName.Text.Trim() == string.Empty)
            {
                X.MessageBox.Configure(new MessageBoxConfig()
                {
                    Icon = MessageBox.Icon.INFO,
                    Title = "提示",
                    Message = "用户真实名称不能为空"
                }).Show();
                return;
            }
            if (tfDepartment.Text.Trim() == string.Empty)
            {
                X.MessageBox.Configure(new MessageBoxConfig()
                {
                    Icon = MessageBox.Icon.INFO,
                    Title = "提示",
                    Message = "部门不能为空"
                }).Show();
                return;
            }

            var model = new Model.ysys_user();
            model.user_name = tfLoginName.Text.Trim();
            model.user_department = tfDepartment.Text.Trim();
            model.user_tname = tfName.Text.Trim();
            model.user_type = ckSupper.Checked ? 1 : 0;

            var bll = new BLL.ysys_user();

            if (tbHidden.Text == "增加")
            {
                if (bll.Add(model) > 0)
                {
                    X.MessageBox.Configure(new MessageBoxConfig()
                    {
                        Icon = MessageBox.Icon.INFO,
                        Title = "提示",
                        Message = "添加用户成功!"
                    }).Show();
                    tfName.Text = string.Empty;
                    tfLoginName.Text = string.Empty;
                    tfDepartment.Text = string.Empty;
                    window1.Hidden = true;
                    ckSupper.Checked = false;
                    grid.Reload();
                }
                else
                {
                    X.MessageBox.Configure(new MessageBoxConfig()
                    {
                        Icon = MessageBox.Icon.INFO,
                        Title = "提示",
                        Message = "添加用户失败!"
                    }).Show();
                    return;
                }
            }
            else if (tbHidden.Text == "修改")
            {
                model.ID = Convert.ToInt32( tbHiddenID.Text);
                if (bll.Update(model))
                {
                    X.MessageBox.Configure(new MessageBoxConfig()
                    {
                        Icon = MessageBox.Icon.INFO,
                        Title = "提示",
                        Message = "修改用户成功!"
                    }).Show();
                    tfName.Text = string.Empty;
                    tfLoginName.Text = string.Empty;
                    tfDepartment.Text = string.Empty;
                    window1.Hidden = true;
                    tbHidden.Text= "增加";
                    ckSupper.Checked = false;
                    grid.Reload();
                }
                else
                {
                    X.MessageBox.Configure(new MessageBoxConfig()
                    {
                        Icon = MessageBox.Icon.INFO,
                        Title = "提示",
                        Message = "修改用户失败!"
                    }).Show();
                    return;
                }
            }
           
        }
    }
}